package com.baoxin.chenxixc4coach.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.baoxin.chenxixc4coach.R;
import com.baoxin.chenxixc4coach.bean.TrainStudent;

import java.util.List;

public class ArrangeTestAdapter extends RecyclerView.Adapter<ArrangeTestAdapter.ViewHolder> {
    private List<TrainStudent> studentList;
    private Context context;
    private boolean multiSelect;
    private ArrangeTestListener arrangeTestListener;

    public ArrangeTestAdapter(Context context, ArrangeTestListener l) {
        this.context = context;
        arrangeTestListener = l;
    }

    public void setData(List<TrainStudent> studentList) {
        this.studentList = studentList;
    }

    public void setMultiSelect(boolean multiSelect) {
        this.multiSelect = multiSelect;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).
                inflate(R.layout.range_student_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int position = holder.getAdapterPosition();
                studentList.get(position).setSelected(isChecked);
            }
        });
        holder.rangeTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                arrangeTestListener.arrange(position);
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        TrainStudent student = studentList.get(position);
        //初始化状态
        if (!multiSelect) {
            holder.checkBox.setVisibility(View.GONE);
            holder.rangeTest.setVisibility(View.VISIBLE);
        } else {
            holder.checkBox.setVisibility(View.VISIBLE);
            holder.rangeTest.setVisibility(View.GONE);
        }

        holder.checkBox.setChecked(student.isSelected());
        holder.name.setText(student.getTrName());
        holder.id.setText(student.getTrIdnumber());
    }

    @Override
    public int getItemCount() {
        return studentList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        View itemView;
        CheckBox checkBox;
        TextView name;
        TextView id;
        TextView rangeTest;

        public ViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            checkBox = itemView.findViewById(R.id.checkbox);
            name = itemView.findViewById(R.id.student_name);
            id = itemView.findViewById(R.id.student_id);
            rangeTest = itemView.findViewById(R.id.set_test_time);
        }
    }

    public interface ArrangeTestListener {
        /**
         * 安排考试
         *
         * @param pos
         */
        void arrange(int pos);
    }
}
